package top.lilele.adminSystem.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import top.lilele.adminSystem.dto.DailyDto;
import top.lilele.adminSystem.dto.PageDto;
import top.lilele.adminSystem.dto.StatisticsDailyDto;
import top.lilele.adminSystem.entity.AdminDailyEntity;
import top.lilele.adminSystem.exception.business.DailyHasFilledException;
import top.lilele.adminSystem.mapper.AdminDailyMapper;
import top.lilele.adminSystem.utils.Utils;
import top.lilele.adminSystem.vo.DailyVo;
import top.lilele.adminSystem.vo.StatisticsDailyVo;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author lilele
 */
@Service
public class AdminDailyService {

    @Resource
    AdminDailyMapper adminDailyMapper;

    @SneakyThrows
    public void add(AdminDailyEntity adminDailyEntity) {
        AdminDailyEntity info = adminDailyMapper.selectOne(new LambdaQueryWrapper<AdminDailyEntity>().eq(AdminDailyEntity::getFillDate, adminDailyEntity.getFillDate()).eq(AdminDailyEntity::getCreatedId, adminDailyEntity.getCreatedId()));
        if (info != null) {
            throw new DailyHasFilledException("当前时间的日报已经填写");
        }
        adminDailyMapper.insert(adminDailyEntity);
    }

    public Object queryDailyList(DailyDto dailyDto, Boolean isPage) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        wrapper.eq(dailyDto.getFillDate() != null, "ad.fillDate", dailyDto.getFillDate());
        // 只查询我的日报,并且按照填报时间降序排列
        if (dailyDto.getType() == 2) {
            wrapper.eq("ad.createdId", StpUtil.getLoginIdAsString());
            wrapper.orderByDesc("ad.fillDate");
        } else {
            wrapper.orderByDesc("ad.fillDate");
        }
        List<Object> list = adminDailyMapper.queryDailyList(wrapper);
        if (isPage) {
            PageDto pageDto = Utils.generatePage(list, dailyDto.getPageNum(), dailyDto.getPageSize());
            return pageDto;
        } else {
            return list;
        }
    }

    public void deleteDailyById(String id) {
        adminDailyMapper.deleteById(id);
    }

    public AdminDailyEntity queryDailyById(String id) {
        AdminDailyEntity info = adminDailyMapper.selectOne(new LambdaQueryWrapper<AdminDailyEntity>().eq(AdminDailyEntity::getId, id));
        return info;
    }

    public void updateDailyById(AdminDailyEntity adminDailyEntity) {
        adminDailyMapper.updateById(adminDailyEntity);
    }

    @SneakyThrows
    public void export(DailyDto dailyDto, HttpServletResponse httpServletResponse) {
        String fileName = "日报报表-" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd-HH-mm-ss");
        List<DailyVo> list = (List<DailyVo>) this.queryDailyList(dailyDto, false);
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.merge(3, "日报报表");
        writer.addHeaderAlias(Utils.getName(DailyVo::getCreatedName), "填报人");
        writer.addHeaderAlias(Utils.getName(DailyVo::getProjectNames), "填报项目");
        writer.addHeaderAlias(Utils.getName(DailyVo::getContent), "填报内容");
        writer.addHeaderAlias(Utils.getName(DailyVo::getCreatedTime), "填报时间");
        writer.setOnlyAlias(true);
        writer.write(list, true);
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        ServletOutputStream out = httpServletResponse.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

    public List<StatisticsDailyVo> queryStatisticsDailyList(StatisticsDailyDto statisticsDailyDto) {
        List<StatisticsDailyVo> list = adminDailyMapper.queryStatisticsDailyList(statisticsDailyDto);
        return list;
    }

    @SneakyThrows
    public void exportStatisticsDaily(StatisticsDailyDto statisticsDailyDto, HttpServletResponse httpServletResponse) {
        String fileName = "日报统计报表-" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd-HH-mm-ss");
        List<StatisticsDailyVo> list = this.queryStatisticsDailyList(statisticsDailyDto);
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.merge(1, "日报统计报表");
        writer.addHeaderAlias(Utils.getName(StatisticsDailyVo::getTrueName), "用户姓名");
        writer.addHeaderAlias(Utils.getName(StatisticsDailyVo::getFillCount), "已填报次数");
        writer.setOnlyAlias(true);
        writer.write(list, true);
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        ServletOutputStream out = httpServletResponse.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}
